package com.lanlian.cayman.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.QuestionAdapter;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.commonlib.bean.QuestionInfo;
import com.lanlian.uikit.RecyclerViewDivider;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/16 10:17
 *     desc   : 常见问题分类TAB
 *     version: 1.0
 * </pre>
 */
public class QuestionFragment extends BaseFragment {

    private RecyclerView mRecycleView;
    private ArrayList<QuestionInfo> mList = new ArrayList<>();
    private QuestionAdapter adapter;

    public static QuestionFragment newInstance(ArrayList<QuestionInfo> list) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("list", list);
        QuestionFragment fragment = new QuestionFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_question, null);
        initView(view);
        initAdapter();
        if (getArguments() != null) {
            mList = (ArrayList<QuestionInfo>) getArguments().getSerializable("list");
            adapter.setNewData(mList);
        }
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mRecycleView = view.findViewById(R.id.recycle_view);
    }

    private void initAdapter() {
        adapter = new QuestionAdapter(R.layout.adapter_question, mList);
        mRecycleView.setAdapter(adapter);
        adapter.bindToRecyclerView(mRecycleView);
        mRecycleView.addItemDecoration(new RecyclerViewDivider(getActivity(), LinearLayoutManager.VERTICAL));
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecycleView.setLayoutManager(linearLayoutManager);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                QuestionAdapter questionAdapter = (QuestionAdapter) adapter;
                if (questionAdapter.booleanArray.get(position)) {
                    questionAdapter.booleanArray.put(position, false);
                } else {
                    questionAdapter.booleanArray.put(position, true);
                }
                questionAdapter.notifyDataSetChanged();
            }
        });
    }

}
